package control
{
	import business.MainBusinessDelegate;
	import business.responder.ContactListReponder;
	
	import conf.Conf;
	
	import model.MainModel;
	
	import mx.managers.CursorManager;
	import mx.rpc.remoting.RemoteObject;
	
	import org.servebox.foundry.control.AbstractController;
	import org.servebox.foundry.model.ModelLocator;

	public class MainController extends AbstractController
	{
		//--------------------------------------------------
		// Singleton implementation
		private static var _instance : MainController;
		
		public static const MAIN_BUSINESS_DELEGATE_ID	: String = "mainBusinessDelegate";
		public static const MAIN_MODEL_ID				: String = "mainModel";
		
		private var _service : RemoteObject;
		
		public static function getInstance() : MainController
		{
			if ( _instance == null )
			{
				_instance = new MainController();
			}
			return _instance;
		}		
 
		//----------------------------------------------------
		// Overriding methods
		override public function initializeModels():void
		{
			// The model can now be referenced from anywhere, using the ModelLocator singleton.
			ModelLocator.getInstance().registerModel( MAIN_MODEL_ID , new MainModel() );
		}
		
		override public function initializeDelegates():void
		{
			var rO : RemoteObject = new RemoteObject( );
			rO.destination = String( Conf.AMFPHP_FACADE );
			rO.source = "IBService";
			//rO.channelSet = ChannelSetProvider.getInstance().getDefaultChannelSet();
			/* var cs : ChannelSet = new ChannelSet( );
			var customChannel : Channel = new AMFChannel( "my-amf", Conf.AMFPHP_HOST + "/xampp/flex/amfphp/gateway.php" );
			cs.addChannel( customChannel );
			customChannel.connect( cs );
			rO.channelSet = cs; */
			_service = rO;
			/* trace( "ID DU ENDPOINT " + AMFChannel( rO.channelSet.channels[0] ).id );
			trace( "ENDPOINT -> " + AMFChannel( rO.channelSet.channels[0] ).endpoint ); */
			registerBusinessDelegate( 	new MainBusinessDelegate( _service ),
		                   				MAIN_BUSINESS_DELEGATE_ID 
		        );
		}
		
		public function getAllContact( ) : void
		{
			getMainBusinessDelegate().getAllContact( new ContactListReponder() );
		}
		
		// TODO : refaire en dynamique en trouvant le moyen de récupérer l'instance de MainBusinessDelegate
		private function getMainBusinessDelegate() : MainBusinessDelegate
		{
			return getBusinessDelegate( MAIN_BUSINESS_DELEGATE_ID ) as MainBusinessDelegate;
		}
	}
}